package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.*;
import com.gec.model.vo.ITripServiceVo;
import com.gec.system.common.Result;
import com.gec.system.mapper.ITripSightsServiceMapper;
import com.gec.system.service.ITripServiceOrderService;
import com.gec.system.service.ITripServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * <p>
 * 套餐表 前端控制器
 * </p>
 *
 * @author dafei
 * @since 2024-01-22
 */
@Api(tags = "套餐管理")
@RestController
@RequestMapping("/system/itrService")
public class ITripServiceController {

    @Autowired
    private ITripServiceService iTripServiceService;

    @Autowired
    private ITripServiceOrderService iTripServiceOrderService;

    @Autowired
    private ITripSightsServiceMapper iTripSightsServiceMapper;

    // 分页 + 查询
    @ApiOperation("套餐分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findRoleByPageQuery(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      ITripServiceVo iTripServiceVo)
    {


        IPage<ITripService> page1 = new Page<>(page,limit);
        page1 =   this.iTripServiceService.selectPage(page1,iTripServiceVo);
        return Result.ok(page1);
    }



    @ApiOperation("uni-app 用户选择套餐")
    @PostMapping("/chooseService")
    public Result chooseService(
        Long id,Long userId
    ) {
        ITripService its = iTripServiceService.getById(id);
        BigDecimal servicePrice = its.getPrice();
        ITripServiceOrder it = new ITripServiceOrder();
        it.setPrice(servicePrice);
        it.setUserId(userId);
        it.setServiceId(id);
        iTripServiceOrderService.save(it);
        return null;
    }


    @ApiOperation("添加套餐")
    @PostMapping("/addService")
    public Result addService(@RequestBody ITripService iTripService){
        //        添加编号唯一性判断  之后换成工作流
        ITripService it = iTripServiceService.selectByName(iTripService.getServiceName());
        if (it != null) {
            return Result.fail("name重复");
        }
        this.iTripServiceService.save(iTripService);
        ITripServiceSights iss = new ITripServiceSights();
        iss.setServiceId(it.getId());
        for (Long id : it.getSightsId()) {
            iss.setSightsId(id);
            iTripSightsServiceMapper.insert(iss);
        }

        return Result.ok();
    }

    @ApiOperation("根据id去查询套餐")
    // 根据id去查询菜单
    @GetMapping("/findNodeById/{id}")
    public Result findNodeById(@PathVariable Long id){
        ITripService iTripService = this.iTripServiceService.getById(id);
        return Result.ok(iTripService);
    }

    @ApiOperation(value = "修改套餐")
    @PostMapping("/updateITripService")
    public Result updateById(@RequestBody ITripService iTripService) {
        iTripServiceService.updateById(iTripService);
        return Result.ok();
    }

    @ApiOperation(value = "删除套餐")
    @DeleteMapping("/removeITripService/{id}")
    public Result removeITripService(@PathVariable Long id) {
        iTripServiceService.removeById(id);
        return Result.ok();
    }

}

